package com.ocom.payconfig.controller;

import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.payconfig.EntityReceiptAddress;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.adreceiptinfo.DeleteAdReceiptInfoRequest;
import com.ocom.common.request.payconfig.adreceiptinfo.GetAdReceiptInfoRequest;
import com.ocom.common.request.payconfig.adreceiptinfo.UpdateAdReceiptInfoRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.SpringUtils;
import com.ocom.common.utils.StringUtils;
import com.ocom.payconfig.service.ReceiptAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 取餐点信息
 * @Author: lvwenxiang
 * @Date: 2023/4/24
 **/

@Slf4j
@RestController
public class ReceiptAddressController {

    @Autowired
    private ReceiptAddressService receiptAddressService;

    @PostMapping(PayConfigUrl.Url_getAdReceiptInfo)
    public Result getAdReceiptInfo(@RequestBody GetAdReceiptInfoRequest request) {
        return ResultUtil.success(receiptAddressService.getAdReceiptInfo(request.getComId(), request));
    }

    @PostMapping(PayConfigUrl.Url_addAdReceiptInfo)
    public Result addAdReceiptInfo(@RequestBody EntityReceiptAddress request) {
        Integer count = receiptAddressService.countByReceiptName(request.getComId(), request.getComId(), request.getReceiptName());

        boolean success = false;
        UpdateAdReceiptInfoRequest updateRequest = new UpdateAdReceiptInfoRequest();
        if (0 == count) {
            Boolean createResult = receiptAddressService.addAdReceiptInfo(request.getComId(), request);
            if (createResult) {
                updateRequest.setReceiptId(request.getReceiptId());
                updateRequest.setReceiptName(request.getReceiptName());
                updateRequest.setComId(request.getComId());
                updateRequest.setParentReceiptId(request.getParentReceiptId());
                updateRequest.setRemark(request.getRemark());
                updateRequest.setUpdateUser(request.getUpdateUser());
                updateRequest.setReceiptOrgin((StringUtils.isEmpty(request.getReceiptOrgin()) ? "0" : request.getReceiptOrgin()) + "," + request.getReceiptId());
                success = receiptAddressService.updateAdReceiptInfo(request.getComId(), updateRequest);
            }
        }
        return success ? ResultUtil.success(updateRequest) : ResultUtil.error(ResultEnum.Err_4069);
    }

    @PostMapping(PayConfigUrl.Url_updateAdReceiptInfo)
    public Result updateAdReceiptInfo(@RequestBody UpdateAdReceiptInfoRequest request) {
        Boolean result = receiptAddressService.updateAdReceiptInfo(request.getComId(), request);
        if (!result) {
            return ResultUtil.error(ResultEnum.Err_3014);
        }
        return ResultUtil.success(request);
    }

    @PostMapping(PayConfigUrl.Url_deleteAdReceiptInfo)
    public Result deleteAdReceiptInfo(@RequestBody DeleteAdReceiptInfoRequest request) {
        Boolean result = receiptAddressService.deleteAdReceiptInfo(request.getComId(), request);
        if (!result) {
            return ResultUtil.error(ResultEnum.Err_2009);
        }
        return ResultUtil.success(request);
    }

    @PostMapping(PayConfigUrl.Url_countByReceiptName)
    public Result countByReceiptName(@RequestParam Long comId, @RequestParam String receiptName) {
        return ResultUtil.success(receiptAddressService.countByReceiptName(comId, comId, receiptName));
    }

    @PostMapping(PayConfigUrl.Url_getByReceiptName)
    public Result getByReceiptName(@RequestParam Long comId, @RequestParam String receiptName) {
        return ResultUtil.success(receiptAddressService.getByReceiptName(comId, comId, receiptName));
    }

}
